Every position in GBML is counted from a top-left corner - of the screen, of the room, of the camera's view or of a background's picture - so (0, 0) is always a top-left corner.
A position is two numbers. x says how far across, and y says how far down. But a number only means something once everyone agrees where the counting starts. That starting spot is called the origin, and it is where x and y are both 0, written (0, 0).
GBML has one rule for it, and it holds everywhere:
(0, 0)is always a top-left corner.
Count pixels right for x and down for y, the way you read a page: start at the top-left corner of the page, go along the line, then down the lines. A bigger x is further right. A bigger y is further down (not up, as on a maths graph).
What changes from place to place is only which top-left corner you mean. There are four of them.
| The number is a place on... | It counts from the top-left of... | Some entries that use it |
|---|---|---|
| the screen | the screen. (0, 0) is the top-left pixel. | every draw call made in a Draw GUI event (draw_text, draw_rect_fill, draw_sprite...), and the _anchored drawing calls anywhere |
| the room | the room. (0, 0) is the room's top-left corner (in a game with a world, the world's). | an instance's x and y, instance_create, pos_free, the tile_ calls, draw_text, draw_sprite, room_left and its three partners, path_x_at, effect_burst |
| the camera | the camera's view, which means the spot where the view begins. | camera_set, camera_x, camera_y, camera_bounds |
| a background | the background's picture. | background_create, draw_background |
One special case: an effect made camera-space (rain or snow that follows the view) counts its start position from the screen's top-left pixel instead of from the room's top-left corner. See effect_start.
On the GBA the screen is 240 pixels wide and 160 tall, so the far corners of the screen are these:
(0, 0)(240, 0) (the last pixel you can see in that row is (239, 0))(0, 160)(240, 160) (the last pixel you can see is (239, 159))So draw_text(0, 0, ...) in a Draw GUI event draws in the top-left corner, and draw_text(240, 0, ...) draws at the top-right corner. Your numbers stay the same on every console. The game turns them into the console's own numbers for you (the GBA itself counts from the middle of its screen): a Draw GUI event draws through a camera of its own that never moves, and that costs nothing while the game runs.
A number can also be negative, or bigger than the far edge. That is allowed and it means "off the screen" or "outside the room". A banner that slides down into view can start at y = -20 and move to y = 0.
Imagine a room that is 320 pixels wide and 320 tall, drawn on a big sheet of graph paper. The top-left corner of the paper is (0, 0). The far right edge is at x = 320, and the bottom edge is at y = 320. (A 320 by 320 room spans 0 to 320 on both sides. It does not run from -160 to 160.)
Now put a treasure chest on the paper at x = 200, y = 100. That is 200 pixels across from the left edge and 100 down from the top edge.
The screen is a window, 240 by 160, that slides over the paper. The camera says where the window's top-left corner is. With camera_set(0, 0) the window sits over the paper's top-left corner and shows x from 0 to 240 and y from 0 to 160. The chest at (200, 100) is inside the window, and it shows up at the same numbers on the screen, (200, 100).
Now slide the window with camera_set(60, 40). Its top-left corner is now over the paper's (60, 40), so it shows x from 60 to 300 and y from 40 to 200. The chest has not moved on the paper. It is still at (200, 100). But it appears on the screen at (200 - 60, 100 - 40), which is (140, 60). That is the rule for anything in the room:
where it appears on the screen = its place in the room - where the camera's view beginsA draw call in a Draw GUI event does not go through the room's camera at all. draw_text(0, 0, "HI") there stays in the screen's top-left corner however far the camera slides. That is what makes it right for a score. (The same call in a Draw event is a place in the room, and does slide.)
x and y say where its origin sits, and the origin is the middle of its sprite unless you chose another spot in the object editor. That has nothing to do with the screen's corner or the room's corner. See Drawing.draw_rect_fill(x, y, w, h, color) puts the rectangle's top-left corner at (x, y), so draw_rect_fill(0, 0, screen_width(), screen_height(), COLOR_BLUE) fills the whole screen. A sprite goes by its own origin, which you pick for it; a rectangle has no origin of its own.speed of 3 or a gap of 8 pixels have no origin. 3 pixels is 3 pixels wherever you start counting.draw_text_anchored and its friends already said "start at this spot, then move this far". Now ANCHOR_TOP_LEFT is simply the same spot as the plain (0, 0), so draw_text_anchored(ANCHOR_TOP_LEFT, 4, 4, s) and draw_text(4, 4, s) draw on the same pixel. Anchors are still how you reach the other eight spots without writing the size of the screen. See Screen anchors.| I want... | Write |
|---|---|
| the room's middle | room_width() / 2 and room_height() / 2 |
| the room's right and bottom edges | room_right() and room_bottom() (they are the same as room_width() and room_height()) |
| an instance 16 pixels in from the room's left edge | x = room_left() + 16; (the same number as x = 16;, but it says what you mean) |
| the camera showing the room's middle | camera_set(room_width() / 2 - screen_width() / 2, room_height() / 2 - screen_height() / 2); |
| the middle of the screen | draw_text_anchored(ANCHOR_CENTER, 0, 0, s), or screen_width() / 2 and screen_height() / 2 |
| a spot in the next room | room_change_ext(room, who, x, y, transition) counts x and y from the top-left of the room you are going to. Use room_width_of(room) / 2 for "the middle of that room". |
Notice that room_width() / 2 is the room's middle, not an edge: the room runs from 0 to room_width().
If your game has a world (one huge map you paint in the Room Editor as if it were a single room, and walk across with no room change you can see), then in that game a position is a place in the world. (0, 0) is the world's top-left corner, and the far corner is as far as the world goes. You never count from a piece of the world, and you never subtract anything:
x and y say where it is in the world, and so do xprev and xstart;camera_x, camera_y and camera_set count in the world, and the camera glides on as the player walks;instance_create, pos_free, tile_free, tile_solid, the other tile_ calls, the drawing calls and effect_start are places in the world, and so are the numbers you get back from tile_ground_y and the box_ variables;room_width and room_height say how big the world is, and room_right and room_bottom are its far edges (room_left and room_top are still 0);dist_to is as it always was, because only the difference between two places matters.An object that keeps a place in a variable (target_x = x;) finds it right again later, wherever the player has walked in the meantime. The one number that is not a place in the world is the one you give room_change_ext: it counts from the room you are going to, which is an ordinary room (a house, a cave).
A world's numbers are big: a world of 50 by 50 screens is 12,000 pixels across and 8,000 down. Numbers on the console hold places up to 500,000, so that is no problem, but a number multiplied by itself (a place squared) is. Work with differences, as dist_to does.
Gbabel used to count from the middle of the room, and a project made back then has every position written that way. You do not have to do anything about it. When you open such a project, Gbabel moves the positions for you: the objects you placed in the rooms, the paths, the cutscenes and the numbers in your code. It keeps a copy of your old files in the folder .gbabel/old-numbers-backup, next to a file called what-moved.txt that lists everything it changed.
A few places it cannot decide by itself: a position kept in a variable and compared with a fixed number later, or a grid you snapped things to by hand. Gbabel leaves those exactly as they were and lists them in a message, with where each one is and what to do. Read that list once and fix the ones that really are positions. Saved games made before the move are not used by the new game, because a position saved the old way would be in the wrong place.
A room that is smaller than the screen (say 224 by 144, or 160 by 128) would show empty space past its right and bottom edges if the camera started at its top-left corner. So whenever a room loads, the camera is pulled back inside the room's limits, and a small room ends up centered on the screen. In that case camera_x can be below 0, because the view begins to the left of the room. A room exactly as big as the screen shows perfectly with the default camera.
dist_to and dir_to take four numbers, x1, y1, x2, y2. They may be two places (counted from the room's top-left corner) or they may be amounts, like dist_to(0, 0, dx, dy) asking how long the arrow (dx, dy) is.
When both points are real positions, such as dist_to(x, y, other.x, other.y), only the difference between them matters. Slide both points the same distance and the answer stays the same, so it does not matter where the counting starts. Two real positions need no change when an old project is moved.
A fixed place written as a number is different. dist_to(0, 0, x, y) asks "how far am I from the top-left corner?", because (0, 0) is that corner and not the middle. To measure from the middle of the room, write the middle: dist_to(room_width() / 2, room_height() / 2, x, y).